﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace WBasicClassLib.ExtFunctions
{
    namespace ExtString
    {
        /// <summary>
        /// 对string的方法扩展集合
        /// </summary>
        public static class ExtString
        {
            #region 中文的全角半角转换

            /// <summary>
            /// 将中文转换成全角字符
            /// </summary>
            /// <param name="Source">原中文字符串</param>
            /// <returns>中文转变为全角字符后的新字符串</returns>
            public static string ToSBC(this string Source)
            {
                char[] c = Source.ToCharArray();
                for (int i = 0; i < c.Length; i++)
                {
                    if (c[i] == 32)
                    {
                        c[i] = (char)12288;
                        continue;
                    }
                    if (c[i] < 127)
                        c[i] = (char)(c[i] + 65248);
                }
                return new string(c);
            }

            /// <summary>
            /// 将中文转换成半角字符
            /// </summary>
            /// <param name="Source">原中文字符串</param>
            /// <returns>中文转变为半角字符后的新字符串</returns>
            public static string ToDBC(this string Source)
            {
                char[] c = Source.ToCharArray();
                for (int i = 0; i < c.Length; i++)
                {
                    if (c[i] == 12288)
                    {
                        c[i] = (char)32;
                        continue;
                    }
                    if (c[i] > 65280 && c[i] < 65375)
                        c[i] = (char)(c[i] - 65248);
                }
                return new string(c);
            }

            #endregion

            #region 获取中文字符中各中文的拼音首字母

            /// <summary>
            /// 获取中文字符串中各中文的拼音首字母，不过不能保证正确识别多音字
            /// </summary>
            /// <param name="Source">原中文字符串</param>
            /// <returns>原字符串中中文字符的拼音首字母字符串</returns>
            public static string GetChineseSpell(this string Source)
            {
                int len = Source.Length;
                string myStr = "";
                for (int i = 0; i < len; i++)
                {
                    myStr += GetSpell(Source.Substring(i, 1));
                }
                return myStr;
            }

            /// <summary>
            /// 用来获得一个汉字的拼音首字母
            /// </summary>
            /// <param name="cnChar">汉字字符</param>
            /// <returns>该汉字字符的拼音首字母</returns>
            private static string GetSpell(string cnChar)
            {
                byte[] arrCN = Encoding.Default.GetBytes(cnChar);
                if (arrCN.Length > 1)
                {
                    int area = (short)arrCN[0];
                    int pos = (short)arrCN[1];
                    int code = (area << 8) + pos;
                    int[] areacode = { 45217,45253,45761,46318,46826,47010,
                                    47297,47614,48119,48119,49062,49324,
                                    49896,50371,50614,50622,50906,51387,
                                    51446,52218,52698,52698,52698,52980,
                                    53689,54481
                                    };
                    for (int i = 0; i < 26; i++)
                    {
                        int max = 55290;
                        if (i != 25) max = areacode[i + 1];
                        if (areacode[i] <= code && code < max)
                        {
                            return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                        }
                    }
                    return "Z";
                }
                else
                    return cnChar;
            }

            #endregion

            #region Reverse函数

            /// <summary>
            /// 返回内容字符次序相反的string
            /// </summary>
            /// <param name="Source">原string对象</param>
            /// <returns>内容字符次序相反的string</returns>
            public static string Reverse(this string Source)
            {
                char[] CharsOfSource = Source.ToCharArray();
                char temp;
                for (int i = 0; i < CharsOfSource.Length / 2; ++i)
                {
                    temp = CharsOfSource[i];
                    CharsOfSource[i] = CharsOfSource[CharsOfSource.Length - 1 - i];
                    CharsOfSource[CharsOfSource.Length - 1 - i] = temp;
                }
                return new string(CharsOfSource);
            }

            #endregion

            #region 查询一个字符串中出现了多少次子串

            /// <summary>
            /// 查询一个字符串中出现了多少次字串
            /// </summary>
            /// <param name="Source">原字符串对象</param>
            /// <param name="SubStr">要参与查询的子串</param>
            /// <returns>子串在原字符串对象中出现的次数</returns>
            public static int NumOfSubString(this string Source, string SubStr)
            {
                int result = 0;
                int index = 0;

                index = Source.IndexOf(SubStr);
                while (index != -1)
                {
                    result++;
                    index++;
                    index = Source.IndexOf(SubStr, index);
                }

                return result;
            }

            #endregion

            #region 开启进程

            /// <summary>
            /// 开启进程
            /// </summary>
            /// <param name="Path">待开启进程的路径</param>
            public static void OpenProcess(this string Path)
            {
                System.Diagnostics.Process.Start(Path);   
            }

            #endregion

            #region 执行DOS命令

            /// <summary>
            /// 执行DOS命令
            /// </summary>
            /// <param name="cmd">要执行的DOS命令</param>
            /// <param name="error">执行命令失败时得到的错误信息</param>
            /// <returns>执行命令后得到的输出</returns>
            public static string ExecuteDOS(this string cmd, out string error)
            {
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("exit");
                error = process.StandardError.ReadToEnd();
                return process.StandardOutput.ReadToEnd();
            }

            #endregion
        }
    }
}
